package es.decloud.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;

/**
 * This class is used to represent available roles in the database.
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *         Version by Dan Kibler dan@getrolling.com
 *         Extended to implement Acegi GrantedAuthority interface
 *         by David Carter david@carter.net
 */
@Entity
@Table(name = "rol")
@NamedQueries({
        @NamedQuery(
                name = "findRolByNombre",
                query = "select r from Rol r where r.nombre = :nombre "
        )
})
public class Rol extends BaseObject implements Serializable, GrantedAuthority {
    
	private static final long serialVersionUID = -6555694126391295196L;

	private Long id;
    private String nombre;
    private String descripcion;

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Rol() {
    }

    /**
     * Create a new instance and set the name.
     *
     * @param name name of the role.
     */
    public Rol(final String nombre) {
        this.nombre = nombre;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    /**
     * @return the name property (getAuthority required by Acegi's GrantedAuthority interface)
     * @see org.springframework.security.core.GrantedAuthority#getAuthority()
     */
    @Transient
    public String getAuthority() {
        return getNombre();
    }

    @Column(length = 20)
    public String getNombre() {
        return this.nombre;
    }

    @Column(length = 64)
    public String getDescripcion() {
        return this.descripcion;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    /**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Rol)) {
            return false;
        }

        final Rol rol = (Rol) o;

        return !(nombre != null ? !nombre.equals(rol.nombre) : rol.nombre != null);

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (nombre != null ? nombre.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.nombre)
                .toString();
    }
}
